# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
### Reducing Prejudice and Support for Religious 
### Nationalism Through Conversations on WhatsApp 

### Author: Rajeshwari Majumdar
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# File:     results_behavioral.R
# Purpose:  analyze decider game and chat game outcomes; 
#           produce Tables C.6, C.7, C.8, C.9, C.10; Figure C.6
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

# %%%%%%%%%%%%%%%%%%%%%%%%%%
# Decider game ----
# %%%%%%%%%%%%%%%%%%%%%%%%%%

## Load data and custom functions 
rm(list=ls())
load("data/Completes_Merged_Anonymized.RData")
source("code/02_setup_functions.R")


## Construct variables ----
### Average allocation to Hindu R in Hindu vs Muslim rounds
d$alloc_HM = ((d$alloc_HM1 + d$alloc_HM2) / 2) 
### Average allocation to one Hindu R in Hindu vs Hindu rounds
d$alloc_HH = ((d$alloc_HH1 + d$alloc_HH2) / 2)
### Deviation from neutral 50 in average allocations
d$alloc_HH.50 = d$alloc_HH - 50
d$alloc_HM.50 = d$alloc_HM - 50   # This is the defined as "Bias"
### Difference in allocation between Anon and Named when Anon is M and when Anon is H
d$alloc_AnonM.AnonH = d$alloc_AnonM - d$alloc_AnonH   # This is defined as "Discrimination" 


## Table C.6: Hindus’ allocations in rounds where both Receivers are named, by partner condition ----
d %>% filter(religion == "Hindu") %>%
  group_by(condition_pair) %>% 
  summarise("N" = length(na.omit(alloc_HM.50)), 
            "Ingroup vs ingroup share (mean)" = mean(na.omit(alloc_HH)),
            "sd_HH" = sd(na.omit(alloc_HH)),
            "Ingroup vs outgroup share (mean)" = mean(na.omit(alloc_HM)),
            "sd_HM" = sd(na.omit(alloc_HM)),
            "Bias" = mean(na.omit(alloc_HM.50))
  )


## Table C.7: Hindus’ allocations in rounds where one Receiver is named, by partner condition ----
d %>% filter(religion == "Hindu") %>%
  group_by(condition_pair) %>% 
  summarise("N" = length(na.omit(alloc_AnonM.AnonH)), 
            "Anon vs ingroup share (mean)" = mean(na.omit(alloc_AnonH)),
            "sd_AnonH" = sd(na.omit(alloc_AnonH)),
            "Anon vs outgroup share (mean)" = mean(na.omit(alloc_AnonM)),
            "sd_AnonM" = sd(na.omit(alloc_AnonM)),
            "Difference" = mean(na.omit(alloc_AnonM.AnonH)))


## Table C.8: Effect of treatment on bias and discrimination outcomes from the Decider Game ----
### First panel: Bias
fn_reg_main(depvar = "alloc_HM.50", data = d)
### Second panel: Discrimination
fn_reg_main(depvar = "alloc_AnonM.AnonH", data = d)


## Effect sizes ----

### Allocation to Anon when Named is M versus when Named is H
#### Control condition (Hindu-Hindu pairs)
t.test(
  d %>% filter(religion == "Hindu" & condition_pair == "HH") %>% select(alloc_AnonM) %>% na.omit() %>% pull(),
  d %>% filter(religion == "Hindu" & condition_pair == "HH") %>% select(alloc_AnonH) %>% na.omit() %>% pull(),
)
#### Treatment condition (Hindu-Muslim pairs)
t.test(
  d %>% filter(religion == "Hindu" & condition_pair == "HM") %>% select(alloc_AnonM) %>% na.omit() %>% pull(),
  d %>% filter(religion == "Hindu" & condition_pair == "HM") %>% select(alloc_AnonH) %>% na.omit() %>% pull(),
)

### Standardized effect size
fn_effectsize(depvar = "alloc_AnonM.AnonH", topic = "all", data = d)


# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Preferences for future interactions activity ("chat game")  ----
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

## Load data and custom functions 
rm(list=ls())
load("data/Completes_Merged_Anonymized.RData")
source("code/02_setup_functions.R")


## Figure C.6: Responses to discussion partner preferences activity ---- 
df_selections = d %>%
  filter(religion == "Hindu", s11.completed == TRUE) %>%
  pivot_longer(
    cols = starts_with("cgr"),
    names_to = "round",
    values_to = "choice"
  ) %>%
  group_by(condition_pair, round, choice) %>%
  summarise(n = n(), .groups = "drop_last") %>%
  mutate(prop = n / sum(n)) %>%
  ungroup() %>%
  mutate(
    round = case_when(
      round == "cgr1" ~ "Round W: Similar Ingroup vs. Different Ingroup (1)",
      round == "cgr2" ~ "Round X: Similar Ingroup vs. Different Ingroup (2)",
      round == "cgr3" ~ "Round Y: Similar Outgroup vs. Different Ingroup (1)",
      round == "cgr4" ~ "Round Z: Similar Outgroup vs. Different Ingroup (2)"
    )
  ) %>%
  mutate(
    choice = case_when(
      choice == "abstain" ~ "No preference",
      choice == "similar" ~ "Similar Ingroup",
      choice == "different" ~ "Different Ingroup",
      choice == "hindu" ~ "Different Ingroup",
      choice == "muslim" ~ "Similar Outgroup"
    )
  )
df_selections$choice = factor(df_selections$choice, levels = c("No preference","Different Ingroup","Similar Ingroup","Similar Outgroup"))
df_selections$condition_pair = ifelse(df_selections$condition_pair=="HH","Hindu-Hindu","Hindu-Muslim")

ggplot(df_selections, aes(fill=choice, y=prop, x=condition_pair)) + 
  geom_bar(position="fill", stat="identity", color = "black") +   
  scale_fill_manual(values = c("blueviolet","cyan3","lightgreen","forestgreen"),name = "Selection") +
  facet_wrap(~round) +
  labs(x = "Pair type", y = "Proportion") +
  theme_bw() 


## Table C.9: Effect of treatment on selections in the discussion partner preferences activity ----

### Create vars representing each round and choice
d = d %>%
  pivot_longer(
    cols = starts_with("cgr"),
    names_to = "cgr",
    values_to = "value"
  ) %>%
  mutate(dummy = 1) %>%
  pivot_wider(
    names_from  = c(cgr, value),
    values_from = dummy,
    values_fill = 0
  )

### Create (Sum: Y, Z) variable, i.e., number of times outgroup selected
d$cgr_muslim_sum = d$cgr3_muslim + d$cgr4_muslim

### List of control variables 
add.vars = c( 
  # Pref vars
  "cgprefage","cgprefhob","cgprefgender","cgprefregion","cgpreflang",
  # Individual demographics
  "age","gender_f","college","incomeamnt",
  "castefw","state_zonal","married",
  # Individual preferences and attitudes
  "socialapps_WhatsApp", 
  "religiosity_score","supportBJP",
  "prejudice_binary",
  # Treatment month
  "Month"
)

### Print full table  
fn_reg_more(depvars = c("cgr1_similar","cgr2_similar","cgr3_muslim","cgr4_muslim","cgr_muslim_sum"), 
              add.controls = add.vars, 
              print = "full",
              data = d)


## Table C.10: Effect of treatment on stated preferences for discussion partners with similar characteristics ----
fn_reg_more(depvars = c("cgprefage","cgprefregion","cgprefgender","cgprefhob","cgpreflang","cgprefreligion"), data = d)


## Effect sizes ----

### Probability of selecting a similar profile when both options are Hindu
d$cgr_similar_mean = (d$cgr1_similar + d$cgr2_similar)/2
d %>% filter(religion == "Hindu", s11.completed == TRUE) %>% group_by(condition_pair) %>% summarize(mean(cgr_similar_mean))

### Probability of selecting a similar Muslim profile vs different Hindu profile
d$cgr_muslim_mean = (d$cgr3_muslim + d$cgr4_muslim)/2
d %>% filter(religion == "Hindu", s11.completed == TRUE) %>% group_by(condition_pair) %>% summarize(mean(cgr_muslim_mean))
fn_effectsize(depvar = "cgr_muslim_mean", topic = "all", data = d)
